package cn.iocoder.yudao.module.system.config.a_gaojian;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CkmConfig {

    @Value("${ckm.ckmPort}")
    private String ckmPort;
    @Value("${ckm.model}")
    private String ckmModel;

    public static String ckm;
    public static String model;
    public static Long revnum = 1000L;
    public static Double threshold = 0.8;

    @PostConstruct
    public void getCkm() {
        ckm = this.ckmPort;
    }

    @PostConstruct
    public void getModel() {
        model = this.ckmModel;
    }

    public List<String> getKeyword(String content, int num) {
        String uri = "/rs/abs/theme";
        HttpRequest request = HttpRequest.post(ckm + uri);
        request.form("text", content)
                .form("num", num);
        HttpResponse response = request.execute();
        if (response.isOk()) {
            String result = response.body();
            JSONObject object = JSON.parseObject(result);
            if (object.getInteger("code") == 1) {
                JSONArray keywords = object.getJSONArray("themeWords");
                List<String> list = new ArrayList<>();
                for (int i = 0; i < keywords.size(); i++) {
                    String word = keywords.getJSONObject(i).getString("word");
                    list.add(word);
                }
                return list;
            } else {
                return new ArrayList<String>();
            }
        } else {
            log.error("getKeyword fail:" + response.body());
            return new ArrayList<String>();
        }
    }

    public String retrieve(String content) throws IOException {
        String serverUri = ckm + "/rs/sim/retrieve";
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "content=" + content + "&model=" + model + "&revnum=" + revnum + "&threshold=" + threshold);
        Request request = new Request.Builder()
                .url(serverUri)
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        Response response = client.newCall(request).execute();
        JSONObject jsonObject = JSONObject.parseObject(response.body().string());
        return jsonObject.get("revNum").toString();
    }

    public String getAbs(String textContent) {
//        String URL = ckm + "/rs/abs";
        String URL = ckm + "/rs/robo/title";
        HashMap<String, Object> paramMap = new HashMap<>();
//        paramMap.put("numOfAbs", 10);
//        paramMap.put("numOfSub", 6);
//        paramMap.put("percent", 5);
        paramMap.put("text", textContent);

        String result = HttpUtil.post(URL, paramMap);
        JSONObject jsonObject = JSONObject.parseObject(result);
        return jsonObject.getString("result");

    }

    public String getTitle(String text) {
        Map<String, Object> contentMap = new HashMap<>();
        String URL = ckm + "/rs/robo/title";
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("text", text);

        String result = HttpUtil.post(URL, paramMap);
        JSONObject jsonObject = JSONObject.parseObject(result);
        int code = jsonObject.getInteger("code");
        if (code == 1) {
            return jsonObject.getString("result");
        } else {
            return "";
        }
    }


    public Boolean update(String content, String id) throws IOException {
        String serverUri = ckm + "/rs/sim/update";
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "content=" + content + "&model=" + model + "&id=" + id);
        Request request = new Request.Builder()
                .url(serverUri)
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        Response response = client.newCall(request).execute();
        return "OK".equals(response.message());
    }

    public String create() throws IOException {
        String serverUri = ckm + "/rs/sim/create";
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "model=" + model);
        Request request = new Request.Builder()
                .url(serverUri)
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        Response response = client.newCall(request).execute();
        JSONObject jsonObject = JSONObject.parseObject(response.body().string());
        return jsonObject.get("code").toString();
    }

}
